 //
//  ViewController+Action.m
//  YouChi
//
//  Created by sam on 15/5/7.
//  Copyright (c) 2015年 Guangzhou QuicklyANT Information Technology Guangzhou QuicklyANT Information Technology Guangzhou QuicklyANT Information Technology Co.,LTD. All rights reserved.
//
#import "UIViewController+Action.h"
#import <objc/runtime.h>
#import "BaseViewController.h"
#import "UIView+Extension.h"

//static char * dir = "dir";
static char * push = "push";
//static char * layout = "layout";
//static char * black = "black";
@implementation UIViewController (Action)

- (IBAction)onReturn
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)toRetionRootViewController
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (IBAction)onDismissReturn
{
    if (self.navigationController.viewControllers>0) {
      
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
    else{
    
        [self dismissViewControllerAnimated:YES completion:nil];
    }
 
}


-(void)toDismissRootViewController
{
    UIViewController *viewController = self;
    while (viewController.presentingViewController) {
        NSLog(@"%@",viewController.presentingViewController);
        //判断是否为最底层控制器
        if ([viewController isKindOfClass:[BaseViewController class]]) {
            viewController = viewController.presentingViewController;
        }else{
            break;
        }
    }
    if (viewController) {
        [viewController dismissViewControllerAnimated:YES completion:nil];
    }

}

- (IBAction)onPopToRootVC
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)presentToVC:(UIViewController *)viewController{

 [self presentViewController:viewController animated:YES completion:nil];
    
    
}

- (void)presentWindowToVC:(UIViewController *)vc IsNaV:(BOOL)isNav Completion:(void (^ __nullable)(void))completion
{
    if (isNav) {
    UINavigationController *nac = [[UINavigationController alloc]initWithRootViewController:vc];
        //必要配置
        vc.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        // 核心代码
        self.definesPresentationContext = YES;
        // 使用其他Style会黑屏
        nac.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        
        //nac.view.backgroundColor = [UIColor clearColor];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:nac animated:YES completion:completion];
    }else
    {
        //必要配置
        vc.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        // 核心代码
        self.definesPresentationContext = YES;
        // 使用其他Style会黑屏
        vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        
        //nac.view.backgroundColor = [UIColor clearColor];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:completion];
    }
    
    
}
- (void)presentWindowToVC:(UIViewController *)vc Completion:(void (^ __nullable)(void))completion
{
    [self presentWindowToVC:vc IsNaV:NO Completion:completion];
}




- (void)pushToVC:(UIViewController *)viewController
{
    if (viewController) {
        [self pushToVC:viewController model:nil];
    }
}
- (void)pushToVC:(UIViewController *)viewController model:(id)model{
    [self pushToVC:viewController model:model hideTabBar:NO];
}

- (void)pushToVC:(UIViewController *)viewController model:(id)model hideTabBar:(BOOL)hide
{
    if (model) {
        viewController.model = model;
    }
   
    if (hide) {
        viewController.hidesBottomBarWhenPushed = YES;
    }

 [self.navigationController pushViewController:viewController animated:YES];

}

- (instancetype )removeNavigationController
{
    if ([self isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nc = (UINavigationController *)self;
        return  nc.topViewController;
    }
    return self;
}

-(UIViewController *)viewControllerForName:(NSString *)name
{
    Class class =NSClassFromString(name);
    
    for (UIViewController *vc in self.navigationController.viewControllers) {
        
        if ([vc isKindOfClass:class]) {
            return vc;
        }else
        {
            return nil;
        }
    }
     return nil;
}

@end





@implementation UIViewController(nib)

- (UIBarButtonItem *)creatBackButton
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *img = [UIImage imageNamed:@"MACKIT.bundle/left"];
    [btn setImage:img forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(onReturn) forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc]initWithCustomView:btn];
    return bbi;
}

- (void)onSetupBackButton
{
    self.navigationController.navigationBar.translucent = NO;
//    if (self.navigationController.topViewController == self) {
//        return;
//    }
    if (self.navigationController.viewControllers.count<=1) {
        return;
    }
    self.navigationItem.leftBarButtonItem = [self creatBackButton];
}


@end


@implementation UIViewController (Segue)
- (UIViewController *)pushedViewController
{
    return objc_getAssociatedObject(self, push);
}

- (void)setPushedViewController:(UIViewController *)pushedViewController
{
    objc_setAssociatedObject(self, push, pushedViewController, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (instancetype )removeNavigationController
{
    if ([self isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nc = (UINavigationController *)self;
        return  nc.topViewController;
    }
    return self;
}
@end


static char * vModel = "vModel";
@implementation UIViewController (create)


- (UIViewController *)getAppRootViewController

{
    
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIViewController *topVC = appRootVC;
    
    while (topVC.presentedViewController) {
        
        topVC = topVC.presentedViewController;
        
    }
    
    return topVC;
}

-(void)setModel:(MABaseModel *)model
{
    objc_setAssociatedObject(self, vModel, model, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(MABaseModel *)model{
    return objc_getAssociatedObject(self, vModel);
}
@end





@implementation UIViewController (Add)

-(void)addView:(id)view{
    if (!view)return;
    [self.view addSubview:view];
}
/** < 设置view的更新数据 > */
-(void)onUpdateSubView:(void (^)(void))loadAnimatedBlock
{
    if (loadAnimatedBlock != self.loadBlock) {
        self.loadBlock = loadAnimatedBlock;
    }
}


@end


@implementation UIViewController (login)

 - (BOOL)gotoLoginPageIfNotLogin
 { 
// NSNumber *user_id = [MCUserDefault currentId];
// NSString *access_token = [MCUserDefault currentToken];
// if (user_id > 0 && access_token.length > 0) {
// MCLoginController *LoginVC = [MCLoginController new];
// LoginVC.hidesBottomBarWhenPushed = YES;
// [self pushToVC:LoginVC];
// return YES;
// } else {
 return NO;
// }
 
 }
 
@end


@implementation UIViewController (TabBar)

-(void)setTabBarItemType:(ETabBarType)type
{
    if (type == kImageTitleType) { // 设置如图片在上,标题在下
        
        {  // 如下写法 offset < 0 图片位置是往上偏的
            CGFloat  offset                   = -2;
            self.tabBarItem.imageInsets = UIEdgeInsetsMake(offset, 0, -offset, 0);
        }
        
        {   // 如下写法 offset < 0 图片位置是往上偏的
            CGFloat  offset                               = -2;
            self.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, offset);
        }
        
    } else if (type == kTitleImageType) { // 设置标题在上,图片在下
        
        {   // 如下写法 offset > 0 图片的位置是往下偏的
            CGFloat  offset                   = 12;
            self.tabBarItem.imageInsets = UIEdgeInsetsMake(offset, 0, -offset, 0);
        }
        
        {  // 如下写法 offset < 0 图片位置是往上偏的
            CGFloat  offset                               = -32;
            self.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, offset);
        }
        
    } else if (type == kImageCenterType) { // 居中显示图片,这里就不能添加标题了,否则很难看
        
        {   // 如下写法 offset > 0 图片的位置是往下偏的 (其实是居中显示了)
            CGFloat  offset                   = 5;
            self.tabBarItem.imageInsets = UIEdgeInsetsMake(offset, 0, -offset, 0);
        }
        
    } else {
        
        // 不设置就是默认
    }

}

@end




